//格式化时间
function dateFtt(fmt, date) { //author: meizz
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function formatSubscribeTime(cellValue, options, rowObject) {
    var dateStr = "";
    if (cellValue) {
        var crtTime = new Date(cellValue);
        dateStr = dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);
    }
    return dateStr;
}

//上报类型字典
function typeFormate(value, options, row) {
    //sex为自定义字典项
    var statusObj = vm.typeObject.reportType;
    console.log(JSON.stringify(statusObj));
    console.log(JSON.stringify(statusObj.length));
    if (value != null && value != '') {
        for (var i = 0; i < statusObj.length; i++) {
            if (statusObj[i].value == value) {
                return '<span>' + statusObj[i].text + '</span>';
            }
        }
    }
}

//上报状态字典
function stateFormate(value, options, row) {
    //sex为自定义字典项
    var statusObj = vm.stateObject.reportState;
    console.log(JSON.stringify(statusObj));
    for (var i = 0; i < statusObj.length; i++) {
        if (statusObj[i].value == value) {
            return '<span>' + statusObj[i].text + '</span>';
        }
    }
}

//查看详情,打开标签
function appInfoDetail(appInfoId) {
    //先关闭菜单实现刷新效果
    parent.closeTabByPageId("999902");
    //id为菜单ID，请从菜单管理中查找，如非配置的菜单则设定个比较大的数为ID，以免与系统菜单冲突。
    parent.addTabs({
        id: '999902',
        title: '查看详情',
        close: true,
        url: 'modules/appReport/appinfoDetail.html?appInfoId=' + appInfoId,
        urlType: 'relative'
    });
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'appReport/appinfo/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id_', width: 50, key: true, hidden: true},
            {label: '上报时间', name: 'time', index: 'time', width: 80, formatter: formatSubscribeTime},
            {
                label: '上报类型',
                name: 'type',
                index: 'type',
                width: 80,
                formatter: function (cellvalue, option, rowObject) {
                    if (cellvalue === '1' || cellvalue === 'jtwf') {
                        return "交通违法";
                    }
                    if (cellvalue === '2' || cellvalue === 'jtsg') {
                        return "交通事故";
                    }
                    if (cellvalue === '3' || cellvalue === 'jtyd') {
                        return "交通拥堵";
                    }
                    if (cellvalue === '4' || cellvalue === 'sssh') {
                        return "设施损坏";
                    }
                    if (cellvalue === '5' || cellvalue === 'zzjb') {
                        return "执法举报";
                    }

                    return "其他";
                }
            },
            {label: '上报地点', name: 'address', index: 'address', width: 80},
            {label: '上报用户', name: 'userid', index: 'user_name', width: 80},
            {label: '联系方式', name: 'userid', index: 'phone', width: 80},
            {label: 'imei', name: 'imei', index: 'imei', width: 80},
            {label: '状态', name: 'state', index: 'state', width: 80, formatter: stateFormate}
           /*{
                label: '详情', name: '', index: '', width: 50,
                formatter: function (cellvalue, option, rowObject) {
                    return '&nbsp;&nbsp;<button class="btn btn-info btn-xs" type="button" onclick=appInfoDetail(\"' + rowObject.id + '\")>详情</button>';

                }
            }*/
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            type: '',
            state: '',
            userid: '',
            address: '',
            userName: ''
        },
        typeObject: {}, //类型集合
        stateObject: {}, //状态集合
        showList: true,
        title: null,
        appInfo: {}
    },
    mounted: function () {
        this.$nextTick(function () {
            this.initSysDict()
        });
    },
    methods: {
        initSysDict: function () {
            $.get(baseURL + "sys/dict/select/reportType", function (r) {
                var flag = r.dict;
                if (r.code === 0) {
                    console.log("before:"+JSON.stringify(flag));
                    for (var i = 0; i < flag.reportType.length; i++) {
                        if(flag.reportType[i].text == "违法"){
                            flag.reportType[i].text = "交通违法"
                        }
                        if(flag.reportType[i].text == "事故"){
                            flag.reportType[i].text = "交通事故"
                        }
                        if(flag.reportType[i].text == "拥堵"){
                            flag.reportType[i].text = "交通拥堵"
                        }
                        if(flag.reportType[i].text == "设施"){
                            flag.reportType[i].text = "设施损坏"
                        }
                        if (flag.reportType[i].text == "执法举报") {
                            flag.reportType.splice(i,1);
                        }
                    }
                    console.log("after:"+JSON.stringify(flag));
                    vm.typeObject = flag;
                } else {
                    console.log(r.msg);
                }
            });

            $.get(baseURL + "sys/dict/select/reportState", function (r) {
                if (r.code === 0) {
                    vm.stateObject = r.dict;
                } else {
                    console.log(r.msg);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        reset: function () {
            vm.q.type = '';
            vm.q.state = '';
            vm.q.address = '';
            vm.q.userid = '';
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.appInfo = {};
        },
        details: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "详情";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.appInfo.id == null ? "appReport/appinfo/save" : "appReport/appinfo/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.appInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "appReport/appinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "appReport/appinfo/info/" + id, function (r) {
                vm.appInfo = r.appInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'type': vm.q.type,
                    'state': vm.q.state,
                    'address': vm.q.address,
                    'userid': vm.q.userid
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});